/**
 * Copyright (C), 2001-${YEAR}, www.bosssof.com.cn
 * @FileName ResourceController.java
 * @Author Administrator
 * @Date 2022-9-28  11:29
 * @Description TODO
 * History:
 * <author> Administrator
 * <time> 2022-9-28  11:29
 * <version> 1.0.0
 * <desc> 版本描述
 */
package com.sd365.permission.centre.api.controller;

import com.sd365.common.core.annotation.stuffer.IdGenerator;
import com.sd365.common.core.common.exception.BusinessException;
import com.sd365.common.core.common.exception.code.BusinessErrorCode;
import com.sd365.common.log.api.annotation.ApiLog;
import com.sd365.common.util.BeanUtil;
import com.sd365.permission.centre.api.ResourceApi;
import com.sd365.permission.centre.pojo.dto.ResourceDTO;
import com.sd365.permission.centre.pojo.query.ResourceQuery;
import com.sd365.permission.centre.pojo.vo.ResourceVO;
import com.sd365.permission.centre.pojo.vo.SubSystemVO;
import com.sd365.permission.centre.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.GsonJsonParser;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
/**
 * @Class ResourceController
 * @Description
 * @Author Administrator
 * @Date 2022-9-28  11:29
 * @version 1.0.0
 */
public class ResourceController implements ResourceApi {
    /**
     *  资源管理类
     */
    @Autowired
    private ResourceService resourceService;
    /**
     *  在基础common包通过雪花算法生成id
     */
    @Autowired
    private IdGenerator idGenerator;


    @Override
    public List<ResourceVO> initRedis(ResourceQuery resourceQuery) {

        if (resourceQuery == null) {
            throw new BusinessException(BusinessErrorCode.SYSTEM_CONTROLLER_ARGUMENT_NOT_VALID, new Exception("查询对象不允许为空"));
        }
        List<ResourceDTO> resourceDTOS = resourceService.initRedis(resourceQuery);
        List<ResourceVO> resourceVOS = BeanUtil.copyList(resourceDTOS, ResourceVO.class);
        return resourceVOS;
    }

    @ApiLog
    @Override
    public List<ResourceVO> commonQuery(ResourceQuery resourceQuery) {
        if (resourceQuery == null) {
            throw new BusinessException(BusinessErrorCode.SYSTEM_CONTROLLER_ARGUMENT_NOT_VALID, new Exception("查询对象不允许为空"));
        }
        List<ResourceDTO> resourceDTOS = resourceService.commonQuery(resourceQuery);
        if (resourceDTOS != null) {
            List<ResourceVO> resourceVOS = BeanUtil.copyList(resourceDTOS, ResourceVO.class, new BeanUtil.CopyCallback() {
                @Override
                public void copy(Object o, Object o1) {
                    ResourceDTO resourceDTO = (ResourceDTO) o;
                    ResourceVO resourceVO = (ResourceVO) o1;

                    if(resourceDTO.getResourceDTO()!=null){
                        ResourceVO copy = BeanUtil.copy(resourceDTO.getResourceDTO(), ResourceVO.class);
                        resourceVO.setResourceVO(copy);
                    }
                    if(resourceDTO.getSubSystem()!=null){
                        BeanUtil.copy(resourceDTO.getSubSystem(), resourceVO.getSubSystemVO(), SubSystemVO.class);
                    }
                }
            });
            return resourceVOS;
        } else return new ArrayList();
    }

    @ApiLog
    @Override
    public List<ResourceVO> queryResourceTree(ResourceQuery resourceQuery) {
        if (resourceQuery == null) {
            throw new BusinessException(BusinessErrorCode.SYSTEM_CONTROLLER_ARGUMENT_NOT_VALID, new Exception("查询对象不允许为空"));
        }
        List<ResourceDTO> resourceDTOS = resourceService.query(resourceQuery);
        List<ResourceVO> resourceVOS = BeanUtil.copyList(resourceDTOS, ResourceVO.class);
        return resourceVOS;
    }

    @ApiLog
    @Override
    public Boolean add(@Valid ResourceDTO resourceDTO) {
        resourceDTO.setId(idGenerator.snowflakeId());
        ResourceVO result = resourceService.add(resourceDTO);
        if(!ObjectUtils.isEmpty(result)){
            return true;
        }
        return false;
    }

    @Override
    public ResourceVO queryResourceById(Long id) {
        ResourceDTO resourceDTO = resourceService.queryById(id);
        if (resourceDTO == null) {
            return new ResourceVO();
        }
        ResourceVO resourceVO = BeanUtil.copy(resourceDTO, ResourceVO.class);
        if(resourceDTO.getResourceDTO()!=null){
            ResourceVO copy = BeanUtil.copy(resourceDTO.getResourceDTO(), ResourceVO.class);
            resourceVO.setResourceVO(copy);
        }

        if(resourceDTO.getSubSystem()!=null){
            SubSystemVO copy = BeanUtil.copy(resourceDTO.getSubSystem(), SubSystemVO.class);
            resourceVO.setSubSystemVO(copy);
        }

        return resourceVO;
    }

    @Override
    public ResourceVO queryResourceByParentId(Long id) {

        ResourceVO resourceVO = BeanUtil.copy(resourceService.findByParentId(id), ResourceVO.class);
        return resourceVO;
    }

    @Override
    public List<ResourceVO> queryResourceByMenu() {

        List<ResourceVO> resourceVOS = BeanUtil.copyList(resourceService.queryResourceByMenu(), ResourceVO.class);
       return resourceVOS;
    }

    @Override
    public Boolean remove(Long id) {
        return resourceService.remove(id);
    }

    @Override
    public Boolean batchDelete(@Valid ResourceDTO[] resourceDTOS) {
        return resourceService.batchDelete(resourceDTOS);
    }

    @Override
    public Boolean modify(@Valid ResourceDTO resourceDTO) {
        ResourceVO result = resourceService.modify(resourceDTO);
        if(!ObjectUtils.isEmpty(result)){
            return true;
        }
        return false;
    }

    @Override
    public ResourceDTO copy(Long id) {
        return resourceService.copy(id);
    }
}
